package com.gxar.quick.ar.server.base.common.utils;

import com.gxar.quick.ar.server.base.common.enums.device.DevicePlatformEnum;
import com.gxar.quick.ar.server.base.common.enums.protocol.ArDeviceLevelEnum;
import com.gxar.quick.ar.server.base.model.protocol.device.CommonDeviceInfo;
import org.apache.commons.lang3.StringUtils;

import java.util.Locale;
import java.util.Objects;

/**
 * 设备工具类
 * <p/>
 * TODO: 完善设备分层方案
 *
 * @author zxh
 * @version 1.0
 * @created 2024/02/23.
 */
public class DeviceLevelUtils {

    // android高端设备系统版本底线
    private static final String ANDROID_HIGH_LEVEL_BOTTOM_LINE_SYSTEM_VERSION = "10";

    // android中端设备系统版本底线
    private static final String ANDROID_MEDIUM_LEVEL_BOTTOM_LINE_SYSTEM_VERSION = "7";

    // iOS高端设备系统版本底线
    private static final String IOS_HIGH_LEVEL_BOTTOM_LINE_SYSTEM_VERSION = "13";

    // iOS中端设备系统版本底线
    private static final String IOS_MEDIUM_LEVEL_BOTTOM_LINE_SYSTEM_VERSION = "9";

    public static ArDeviceLevelEnum getDeviceLevel(CommonDeviceInfo deviceInfo) {
        if (Objects.isNull(deviceInfo)) {
            return ArDeviceLevelEnum.HIGH;
        }

        //
        if (DevicePlatformEnum.ANDROID.equals(deviceInfo.getPlatform())) {
            return getAndroidDeviceLevel(deviceInfo);
        } else if (DevicePlatformEnum.IOS.equals(deviceInfo.getPlatform())) {
            return getIOSDeviceLevel(deviceInfo);
        } else {
            // 其他平台暂不优化
            return ArDeviceLevelEnum.HIGH;
        }
    }

    private static ArDeviceLevelEnum getAndroidDeviceLevel(CommonDeviceInfo deviceInfo) {
        // 通过系统版本判别, 待完善
        if (StringUtils.isNotBlank(deviceInfo.getSystem())) {
            String pureVersion = extractVersion(deviceInfo.getSystem());

            if (compareVersion(pureVersion, ANDROID_HIGH_LEVEL_BOTTOM_LINE_SYSTEM_VERSION) >= 0) {
                return ArDeviceLevelEnum.HIGH;
            } else if (compareVersion(pureVersion, ANDROID_MEDIUM_LEVEL_BOTTOM_LINE_SYSTEM_VERSION) >= 0) {
                return ArDeviceLevelEnum.MEDIUM;
            } else {
                return ArDeviceLevelEnum.LOW;
            }
        }

        if (Objects.nonNull(deviceInfo.getBenchmarkLevel()) && deviceInfo.getBenchmarkLevel() > 0) {
            if (deviceInfo.getBenchmarkLevel() <= 15) {
                return ArDeviceLevelEnum.LOW;
            } else if (deviceInfo.getBenchmarkLevel() <= 30) {
                return ArDeviceLevelEnum.MEDIUM;
            } else {
                return ArDeviceLevelEnum.HIGH;
            }
        }

        return ArDeviceLevelEnum.HIGH; // 不优化
    }

    private static ArDeviceLevelEnum getIOSDeviceLevel(CommonDeviceInfo deviceInfo) {
        // 通过系统版本判别, 待完善
        if (StringUtils.isNotBlank(deviceInfo.getSystem())) {
            String pureVersion = extractVersion(deviceInfo.getSystem());

            if (compareVersion(pureVersion, IOS_HIGH_LEVEL_BOTTOM_LINE_SYSTEM_VERSION) >= 0) {
                return ArDeviceLevelEnum.HIGH;
            } else if (compareVersion(pureVersion, IOS_MEDIUM_LEVEL_BOTTOM_LINE_SYSTEM_VERSION) >= 0) {
                return ArDeviceLevelEnum.MEDIUM;
            } else {
                return ArDeviceLevelEnum.LOW;
            }
        }

        return ArDeviceLevelEnum.HIGH; // 不优化
    }

    /**
     * 提取版本号
     *
     * @param inputVersion 输入版本号
     * @return
     */
    private static String extractVersion(String inputVersion) {
        if (StringUtils.isBlank(inputVersion)) {
            return "0";
        }

        // Android 14
        // iOS 12.1
        return inputVersion.toLowerCase(Locale.ROOT)
                .replace("android", "")
                .replace("ios", "")
                .trim();
    }

    /**
     * 比较系统版本号
     *
     * @param version1 输入版本1
     * @param version2 输入版本2
     * @return 比较结果, 0:一致, 1: 版本1>版本2, -1: 版本1<版本2
     */
    static int compareVersion(String version1, String version2) {
        if (version1.equals(version2)) {
            return 0;
        }

        String[] v1 = version1.split("\\.");
        String[] v2 = version2.split("\\.");

        int length = Math.max(v1.length, v2.length);
        for (int i = 0; i < length; i++) {
            int num1 = i < v1.length ? Integer.parseInt(v1[i]) : 0;
            int num2 = i < v2.length ? Integer.parseInt(v2[i]) : 0;

            if (num1 < num2) {
                return -1;
            } else if (num1 > num2) {
                return 1;
            }
        }

        return 0;
    }

    private DeviceLevelUtils() {
        // Nothing
    }
}
